using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GameAOT;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 场景上固定的显示对象，根据角色的行动或玩家手势，可能会做一些纯表现的效果
    /// 比如桥上的鸟，走过去就会飞走
    /// </summary>
    public class SceneDisplayObject : IReusableClass, IBVHObject2D
    {
        public uint MaxStore => 100;

        public GameObject gameObject { get; private set; }

        public Bounds triggerRange { get; private set; }
        public SceneObjectTriggerActionType actionType { get; private set; }
        public bool restoreOnFarAway { get; private set; }
        public BVHBounds2D bvhBounds { get; set; }

        public Vector3 position => gameObject.transform.position;
        public bool isTriggerRotate { get; private set; }

        private static readonly int ImportantEntityLayer = LayerMask.NameToLayer("ImportantEntity");
        private int _originalLayer;
        private BehaviorFeatureComp _animationCom;
        private bool _isPlayed;

        public bool Init(GameObject gameObject, Bounds triggerRange, SceneObjectTriggerActionType actionType, bool restoreOnFarAway)
        {
            this.gameObject = gameObject;
            this._originalLayer = gameObject.layer;
            this.triggerRange = triggerRange;
            this.actionType = actionType;
            this.restoreOnFarAway = restoreOnFarAway;
            if (actionType == SceneObjectTriggerActionType.PlayAnimation)
            {
                _animationCom = gameObject.GetComponentInChildren<BehaviorFeatureComp>(true);
                if (_animationCom == null)
                {
                    LogGame.LogError($"场景显示对象触发的动作类型为[{actionType}]，但该对象上找不到组件[BehaviorFeatureComp]，对象名：{gameObject.name}");
                    return false;
                }
            }
            if (restoreOnFarAway)
            {
                ResetAction();
            }
            return true;
        }

        public bool IsInHeightRange(float y)
        {
            Vector3 triggerCenter = this.gameObject.transform.position + triggerRange.center;
            float minY = triggerCenter.y - triggerRange.extents.y;
            float maxY = triggerCenter.y + triggerRange.extents.y;
            return y >= minY && y <= maxY;
        }

        public bool DoAction()
        {
            if (_isPlayed)
                return false;
            _isPlayed = true;
            LogGame.Log("DoAction");
            switch (actionType)
            {
                case SceneObjectTriggerActionType.HilightOnCovered:
                    gameObject.SetLayerRecursively(ImportantEntityLayer);
                    break;
                case SceneObjectTriggerActionType.PlayAnimation:
                    _animationCom.Play();
                    break;
                default:
                    break;
            }
            return true;
        }

        public bool DoRevertAction()
        {
            if (!_isPlayed)
                return false;
            _isPlayed = false;
            LogGame.Log("DoRevertAction");
            switch (actionType)
            {
                case SceneObjectTriggerActionType.HilightOnCovered:
                    gameObject.SetLayerRecursively(_originalLayer);
                    break;
                case SceneObjectTriggerActionType.PlayAnimation:
                    _animationCom.StopAndRestore();
                    break;
                default:
                    break;
            }
            return true;
        }

        public void ResetAction()
        {

        }

        public void OnReset()
        {
            this.gameObject = null;
            this._animationCom = null;
            this._isPlayed = false;
        }
    }
}
